#!/bin/bash
#配置内核参数，将桥接的IPv4流量传递到iptables的链
Kernel
#安装常用的软件
yum_install
#设置镜像加速器
reload
#安装kubectl、kubelet、kubeadm
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#安装kubectl、kubelet、kubeadm
yum -y install kubectl-1.18.0 kubelet-1.18.0 kubeadm-1.18.0
systemctl enable kubelet --now
status=`systemctl  status kubelet|grep Active|awk '{print $3}'|cut -d '(' -f2 | cut -d ')' -f1`
[ $? -eq 0 ] && echo -e "\033[32m kubelet install success \033[0m" || \
echo -e "\033[33m kubelet install fail \033[0m"
exit 11
#初始化k8s集群(master节点)
#初始化集群的时候cpu最低是两核
cpu1=`cat /proc/cpuinfo |grep "processor" |sort |uniq |wc -l`
if [ $cpu1 -lt 2 ];then
	echo "Failed initialization,Please check whether the number of CPU cores meets the minimum requirements"
else
kubeadm init --try-run
fi

kubeadm_init(){
local hosts=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep -v 172.17.0.1|awk '{print $2}'|tr -d "addr:"|head -1`
kubeadm init --kubernetes-version=1.18.0  \
--apiserver-advertise-address=$hosts  \
--image-repository registry.aliyuncs.com/google_containers  \
--service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.0.0/16 > log/init.log
#node节点不需要下面的步骤，后续会写playbook自动部署
#捉急的时候，可以把上面的脚本复制一份，在各个node节点执行
#记录后面的kubeadm join这段内容，此内容需要在其它节点加入Kubernetes集群时执行。
#kubeadm join 192.168.137.110:6443 --token o11ulw.ovtj9vov7ob34hs7 \
#   --discovery-token-ca-cert-hash #sha256:38a93626ac4e86155583e7ef9b32cb13739d5f5bc3da2b4ed7e74aec8112bea7
}
install_kubectl(){
#创建kubectl
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#设置kubectl的自动补齐
kubectl completion bash >/etc/bash_completion.d/kubectl
kubeadm completion bash >/etc/bash_completion.d/kubeadm
#不配置$HOME/.kube/config的话，kubectl命令不可用，
#node节点写法有点不一样，node节点的这行为：sudo cp -i /etc/kubernetes/kubelet.conf.conf $HOME/.kube/config
}
#安装calico网络（master节点）
install_calico(){
network=`ping -c 3 www.baidu.com &>/dev/null`
hosts=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep -v 172.17.0.1|awk '{print $2}'|tr -d "addr:"|head -1`
basepath=$(cd `dirname $0`;pwd)
port=5000
#需要提前安装好docker镜像仓库，在init.sh脚本中会更新（尚未完成）
if [ $network -eq 0 ];then
    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
else
    [ -f $basepath/conf/calico.yaml ] &&sed -i 's#docker.io/calico#${hosts}:${port}#g' $basepath/conf/calico.yaml
    kubectl apply -f $basepath/conf/calico.yaml
fi
}
#安装calico网络网络后过一会再输入kubectl get node，可以看到节点的STATUS由NotReady变为Ready
#node节点加入集群（node节点）
#kubeadm join 192.168.137.110:6443 --token o11ulw.ovtj9vov7ob34hs7     --discovery-token-ca-cert-hash sha256:38a93626ac4e86155583e7ef9b32cb13739d5f5bc3da2b4ed7e74aec8112bea7
#kubeadm init后得到的token有效期为24小时，过期后需要重新创建token
#执行：kubeadm token create --ttl=0 --print-join-command获取新token
#kubeadm token list 查看token列表，
#sha256的token值
#openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt |openssl rsa -pubin -outform der |openssl dgst -sha256 -hex
 
